﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using SharedContent;

namespace GlobalGameJam
{
    class Gravite
    {
        public Player   player;
        public float    timer;
        public int      direction;
        public List<Plateform> plat;
        public int tempY;
        public GameplayScreen _game;
        public Vector2  prev_position;

        public Gravite(Player player, List<Plateform> plat, GameplayScreen game)
        {
            _game = game;
            this.player = player;
            this.timer = 0;
            this.plat = plat;
            this.setDir(0);
        }

        public void setDir(int dir)
        {
            this.direction = dir;
            player.direction = dir;
        }

        public bool isFalling()
        {
            if (timer > 0)
                return true;
            return false;
        }

        public void checkGravite(GameTime gametime)
        {

            switch (this.direction)
            {
                case 0:
                    GraviteNormal(gametime);
                    break;
                case 1:
                    GraviteInverse(gametime);
                    break;
                case 2:
                    GraviteGauche(gametime);
                    break;
                case 3:
                    GraviteDroite(gametime);
                    break;
                default:
                    GraviteNormal(gametime);
                    break;
            }
        }
        public void GraviteDroite(GameTime gametime)
        {
            timer += gametime.ElapsedGameTime.Milliseconds;

            Console.WriteLine(timer);
            player.checkSlow(gametime);
//            if (player.position.X < player.max.X - player.sprite.Width * 0.3f - 230)
            //if (player.position.X < player.max.X - 100 * 0.3f - 230)
            //{
            //    player.slow();
            //}
            if (player.position.X < player.max.X)
            {
                Vector2 pos = player.position;
                player.position.X = (float)(player.position.X) + 1 * timer / 10;
                player.UpdateHitbox();
//                if (player.position.X >= player.max.X - player.sprite.Width * 0.3f)
                if (player.position.X >= player.max.X - 100)  
                {
                    timer = 0;

                }
                else if (_game.collisionManager.thereIsObstacle(player))
                {
                    player.position = pos;
                    timer = 0;
                }
            }
        }
        public void GraviteGauche(GameTime gametime)
        {
            timer += gametime.ElapsedGameTime.Milliseconds;

            Console.WriteLine(timer);
            player.checkSlow(gametime);
//            if (player.position.X > 0 + player.sprite.Width * 0.3f + 230)
            //if (player.position.X > 0 + 100 * 0.3f + 230)
            //{
            //    player.slow();
            //}
            if (player.position.X > 0)
            {
                Vector2 pos = player.position;
                player.position.X = (float)(player.position.X) - 1 * timer / 10;
                player.UpdateHitbox();
                if (player.position.X <= 0)
                {
                    timer = 0;

                }
                else if (_game.collisionManager.thereIsObstacle(player))
                {
                    player.position = pos;
                    timer = 0;
                }
            }
        }
        public void GraviteInverse(GameTime gametime)
        {
            timer += gametime.ElapsedGameTime.Milliseconds;

            Console.WriteLine(timer);
            player.checkSlow(gametime);
//            if (player.position.Y > 0 + player.sprite.Height * 0.3f + 230)
            //if (player.position.Y > 0 + 100 * 0.3f + 230)  
            //{
            //    player.slow();
            //}
            if (player.position.Y > 0)
            {
                Vector2 pos = player.position;
                player.position.Y = (float)(player.position.Y) - 1 * timer / 10;
                player.UpdateHitbox();
                if (player.position.Y <= 0)
                {
                    timer = 0;

                }
                else if (_game.collisionManager.thereIsObstacle(player))
                {
                    player.position = pos;
           
                    timer = 0;
                }
            }
        }
        public void GraviteNormal(GameTime gametime)
        {
            timer += gametime.ElapsedGameTime.Milliseconds;

            
            player.checkSlow(gametime);


            //if (player.position.Y < player.max.Y - player.todraw.sprite.Height - 230)
            //{
            //   player.slow();
            //}

                Vector2 pos = player.position;
                player.position.Y = (float)(player.position.Y) + 1 * timer / 10;
                Console.WriteLine("player:" + player.position.X + "-" + player.position.Y + " [] "+prev_position.Y);
                if (timer >= 120.0f && player.sprite._state != SpriteManager.State.FallLeft
                    && player.sprite._state != SpriteManager.State.FallRight)//((float)player.position.Y - (float)prev_position.Y) > 1.55f)
                {
                    if (!this.player.sprite._isRight)
                        player.sprite.setState(SpriteManager.State.FallLeft);
                    else
                        player.sprite.setState(SpriteManager.State.FallRight);
                }
                prev_position = player.position;
                player.UpdateHitbox();
                //                if (player.position.Y >= player.max.Y - player.sprite.Height)
                player.position = pos;
                if (_game.collisionManager.thereIsObstacle(player))
                {
                    player.position = pos;
                    timer = 0;
                }
                else
                    _game.collisionManager.allUp(timer / 10);
        }
    }
}
